// index.js
// 获取应用实例
import Notify from '@vant/weapp/notify/notify';
const app = getApp()
Page({
  data: {
    //搜索内容
    value: '',
    //标签内容
    active: 1,
    //所有文章中的变量
    TabCur: 0,
    scrollLeft: 0,
    //文章分类列表
    type: [{
      id: 0,
      name: '所有文章'
    }],
    //分页查询变量初始值
    offset: 0,
    score: Date.parse(new Date()),
    passages: [],
    likes: [],
    love: [],
    loadModal: true
  },
  //所有文章的分类
  tabSelect(e) {
    //切换分类时，把时间设置为当前时间戳，offset设置为0,清空文章列表
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60,
      passages: [],
      offset: 0,
      score: Date.parse(new Date())
    })
    if (this.data.TabCur == 0) {
      this.getAllPagssagesByOffsetAndScore()
    } else {
      this.getPassageByKindAndOffsetAndScore()
    }
  },
  onLoad() {},
  onShow() {
    this.setData({
      love: wx.getStorageSync('likes')
    })
    //获取所有分类
    this.getAllKind()
    //初始化，获取全部文章的第一页
    this.getAllPagssagesByOffsetAndScore()
    this.getLikes()
  },
  //获取所有文章分类
  getAllKind() {
    let kind = wx.getStorageSync('kind')
    if (kind == null || kind == '') {
      app.request({
        url: app.globalData.apiUrl + "/passage/kind",
        method: 'GET',
      }).then(res => {
        if (res.statusCode == 200) {
          wx.setStorageSync('kind', res.data.data)
          this.setData({
            type: this.data.type.concat(res.data.data)
          })
        } else {
          Notify('服务错误，稍后重试');
        }
      })
    } else {
      this.setData({
        type: this.data.type.concat(kind)
      })
    }
  },
  //根据时间获取所有文章
  getAllPagssagesByOffsetAndScore() {
    if (this.data.offset == null || '') {
      console.log('没有过呢更多啦')
    } else {
      app.request({
        url: app.globalData.apiUrl + '/passage/all/' + this.data.offset + '/' + this.data.score,
        method: 'GET',
      }).then(res => {
        if (res.statusCode == 200) {
          this.setData({
            passages: this.data.passages.concat(res.data.data.list),
            offset: res.data.data.offset,
            score: res.data.data.minTime,
            loadModal: false
          })
          console.log('score为' + this.data.score)
        } else {
          this.setData({
            loadModal: false
          })
          Notify('服务错误，稍后重试');
        }
      })
    }
  },
  //根据类型、时间获取所有文章
  getPassageByKindAndOffsetAndScore() {
    app.request({
      url: app.globalData.apiUrl + '/passage/' + this.data.TabCur + '/' + this.data.offset + '/' + this.data.score,
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          passages: res.data.data.list == null ?
            this.data.passages : this.data.passages.concat(res.data.data.list),
          offset: res.data.data.offset,
          score: res.data.data.minTime,
          loadModal:false
        })
      } else {
        this.setData({
          loadModal:false
        })
        Notify('服务错误，稍后重试');
      }
    })

  },
  //分页获取文章，注意判断TabCur是否为0,决定调用哪个方法
  onReachBottom: function () {
    if (this.data.score == null || '' || undefined) {
      console.log('没有过呢更多啦')
    } else {
      if (this.data.TabCur == 0) {
        this.getAllPagssagesByOffsetAndScore()
      } else {
        this.getPassageByKindAndOffsetAndScore()
      }
    }

  },
  //跳转到文章页面
  gotoPassage(e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/index/passage/passage?id=' + e.currentTarget.dataset.id,
    })
  },
  //获取可能喜欢的文章
  getLikes() {
    app.request({
      url: app.globalData.apiUrl + '/passage/likes',
      data: {
        list: this.data.love
      },
      method: 'POST',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          likes: res.data.data,
          loadModal:false
        })
      } else {
        this.setData({
          loadModal:false
        })
      }
    })
  }
})